import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:my_app_01/utils/index.dart';
import 'package:nil/nil.dart';

class TextInputPage extends StatefulWidget {
  String label;
  dynamic value;
  String? type;

  TextInputPage({super.key, required this.label, this.value, this.type = 'text'});

  @override
  State<TextInputPage> createState() => _TextInputPageState();
}

class _TextInputPageState extends State<TextInputPage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    TextEditingController _uController = TextEditingController.fromValue(
      TextEditingValue(
        text: widget.value ?? "",
        selection:
            TextSelection.fromPosition(TextPosition(affinity: TextAffinity.downstream, offset: widget.value!.length)),
      ),
    );

    return Scaffold(
      backgroundColor: Colors.white70,
      appBar: AppBar(
        surfaceTintColor: Colors.white,
        elevation: 6,
        shadowColor: Colors.black26,
        title: Text("设置${widget.label!}"),
        centerTitle: true,
      ),
      body: Container(
        width: double.infinity,
        height: 60,
        padding: const EdgeInsets.only(left: 20, right: 20),
        margin: const EdgeInsets.only(top: 20, bottom: 20),
        decoration: const BoxDecoration(color: Colors.white),
        child: Column(
          children: [
            Row(
              children: [
                Expanded(
                  flex: 1,
                  child: Text(widget.label!),
                ),
                Expanded(
                  flex: 4,
                  child: buildValueCpm(_uController, context),
                )
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget buildValueCpm(TextEditingController _uController, BuildContext context) {
    if (widget.type == 'text') {
      return TextField(
        autofocus: true,
        controller: _uController,
        decoration: InputDecoration(
            hintText: "请输入${widget.label}",
            contentPadding: const EdgeInsets.all(15),
            border: InputBorder.none,
            enabledBorder: InputBorder.none,
            suffixIcon: widget.value!.isNotEmpty
                ? IconButton(
                    onPressed: () {
                      setState(() {
                        widget.value = "";
                      });
                      _uController.clear();
                    },
                    icon: const Icon(Icons.cancel, color: Colors.black26))
                : nil),
        onChanged: (value) {
          setState(() {
            widget.value = value;
          });
        },
        onSubmitted: (value) {
          MUtils.showCupertinoAlertDialog(
            context,
            title: "保存${widget.label}信息",
            content: "确定要保存${widget.label}吗？",
            callback: (value) {
              if (value) {
                Navigator.of(context).pop({"value": widget.value});
              }
            },
          );
        },
      );
    } else if (widget.type == 'radio') {}
    return nil;
  }
}
